package hpc.exercices;

import org.jocl.CL;
import org.jocl.Pointer;
import org.jocl.Sizeof;
import org.jocl.cl_event;

public class Profiler {

	/*
	 * Method to profile kernel execution time
	 */
	public static long profile(cl_event event, boolean write) {
		long startTime[] = new long[1];
		long endTime[] = new long[1];

		CL.clGetEventProfilingInfo(event, CL.CL_PROFILING_COMMAND_START,
				Sizeof.cl_ulong, Pointer.to(startTime), null);
		CL.clGetEventProfilingInfo(event, CL.CL_PROFILING_COMMAND_END,
				Sizeof.cl_ulong, Pointer.to(endTime), null);

		// convert nano-seconds to ms and print it, divide by 1 000 000
		long duration = endTime[0] - startTime[0];
		if (write) {
			 System.out.println("Duration: "+
			 String.format("%8.3f", duration / 1e6)+" ms");
		}
		return duration;
	}
}
